package com.wugh.fast.demo.config;

import com.wugh.fast.module.cache.TtlCacheResolver;
import com.wugh.fast.module.cache.TtlRedisCacheManager;
import com.wugh.fast.module.cache.util.FastCacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.time.Duration;

/**
 * 说明:
 *
 * @author wugh
 * @date 2019-07-29 11:15
 */
@EnableCaching
@Configuration
public class CacheConfig implements CachingConfigurer {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Override
    public CacheManager cacheManager() {
        return new TtlRedisCacheManager(RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory), FastCacheUtils.getRedisCacheConfigurationWithTtl(Duration.ofHours(1)));
    }

    @Override
    public CacheResolver cacheResolver() {
        TtlCacheResolver resolver = new TtlCacheResolver();
        resolver.setCacheManager(cacheManager());
        return resolver;
    }

    @Override
    public KeyGenerator keyGenerator() {
        return null;
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return null;
    }

}
